package com.redpill.linpro.transaction.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.SimpleTransactionStatus;

@Repository
public class PlatformTransactionManagerStub implements PlatformTransactionManager {

private final Logger logger = LoggerFactory.getLogger(PlatformTransactionManagerStub.class);
	
	private int beginTransactionCounter = 0;
	private int commitTransactionCounter = 0;
	private int rollbackTransactionCounter = 0;
	
	@Override
	public void commit(TransactionStatus status) throws TransactionException {
		commitTransactionCounter++;
		logger.debug("Commit transaction");
	}

	@Override
	public TransactionStatus getTransaction(TransactionDefinition definition)
			throws TransactionException {
		beginTransactionCounter++;
		logger.debug("Begin transaction");	
		SimpleTransactionStatus simpleTransactionStatus = new SimpleTransactionStatus();
		return simpleTransactionStatus;
	}

	@Override
	public void rollback(TransactionStatus status) throws TransactionException {
		rollbackTransactionCounter++;
		logger.debug("Rollback transaction");
	}
	
	public int getBeginTransactionCounter() {
		return beginTransactionCounter;
	}
	
	public int getCommitTransactionCounter() {
		return commitTransactionCounter;
	}
	
	public int getRollbackTransactionCounter() {
		return rollbackTransactionCounter;
	}
	
	public void resetCounters() {
		beginTransactionCounter = commitTransactionCounter = rollbackTransactionCounter = 0;
	}

}
